<template>
  <div class="app-container">
    <!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
    <el-form ref="queryForm" :model="queryParams" label-width="99px" v-show="showSearch" class="ele-form-search"
             @keyup.enter.native="reload"
             @submit.native.prevent>
      <el-row :gutter="15">
        <el-col :lg="6" :md="12">
          <!--    <el-form-item label="产品编号" prop="materialNumber">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.materialNumber"-->
          <!--          placeholder="请输入产品编号"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
        </el-col>
        <el-col :lg="6" :md="12">
          <el-form-item label="产品名称" prop="materialName">
            <el-input
              v-model="queryParams.materialName"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="12">
          <!--      <el-form-item label="数量" prop="quantity">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.quantity"-->
          <!--          placeholder="请输入数量"-->
          <!--          clearable-->
          <!--          @input="aa"-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
        </el-col>
        <el-col :lg="6" :md="12">
          <el-form-item label="库位编号" prop="locationNumber">
            <el-input
              v-model="queryParams.locationNumber"
              placeholder="请输入库位编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="12">
          <el-form-item label="动作" prop="movement">
            <el-input
              v-model="queryParams.movement"
              placeholder="请输入动作"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
          <!--      <el-form-item label="来源单号" prop="trackingNumber">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.trackingNumber"-->
          <!--          placeholder="请输入来源单号"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
        </el-col>
        <el-col :lg="8" :md="12">
          <!--      <el-form-item label="创建人" prop="createUser">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.createUser"-->
          <!--          placeholder="请输入创建人"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
        </el-col>
        <el-col :lg="8" :md="12">
          <!--      <el-form-item label="最后更新人" prop="lastUpdater">-->
          <!--        <el-input-->
          <!--          v-model="queryParams.lastUpdater"-->
          <!--          placeholder="请输入最后更新人"-->
          <!--          clearable-->
          <!--          @keyup.enter.native="handleQuery"-->
          <!--        />-->
          <!--      </el-form-item>-->
          <!--        </el-col>-->
          <!--        <el-col :lg="8" :md="12">-->
          <!--      <el-form-item label="最后更新时间" prop="lastUpdateTime">-->
          <!--        <el-date-picker clearable-->
          <!--          v-model="queryParams.lastUpdateTime"-->
          <!--          type="date"-->
          <!--          value-format="yyyy-MM-dd"-->
          <!--          placeholder="请选择最后更新时间">-->
          <!--        </el-date-picker>-->
          <!--      </el-form-item>-->
        </el-col>

      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['wareManagement:inventoryJournal:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['wareManagement:inventoryJournal:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['wareManagement:inventoryJournal:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wareManagement:inventoryJournal:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryJournalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="库存流水id" align="center" prop="journalId" />-->
      <el-table-column label="产品编号" align="center" prop="materialNumber"/>
      <el-table-column label="产品名称" align="center" prop="materialName"/>
      <el-table-column label="数量" align="center" prop="quantity"/>
      <el-table-column label="库位编号" align="center" prop="locationNumber"/>
      <el-table-column label="产品类型" align="center" prop="type"/>
      <el-table-column label="动作" align="center" prop="movement"/>
      <el-table-column label="来源单号" align="center" prop="trackingNumber"/>

      <el-table-column label="最后更新人" align="center" prop="lastUpdater"/>
      <!--      <el-table-column label="最后更新时间" align="center" prop="lastUpdateTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['wareManagement:inventoryJournal:edit']"-->
      <!--          >修改</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['wareManagement:inventoryJournal:remove']"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存流水查询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="102px">
        <el-row :gutter="15">
          <el-col :lg="12" :md="24">
            <el-form-item label="产品编号" prop="materialNumber">
              <el-input v-model="form.materialNumber" placeholder="请输入产品编号"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="产品名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入产品名称"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="数量" prop="quantity">
              <el-input @input="bb" v-model="form.quantity" placeholder="请输入数量"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="库位编号" prop="locationNumber">
              <el-input v-model="form.locationNumber" placeholder="请输入库位编号"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="产品类型" prop="type">
              <el-input v-model="form.type" placeholder="请输入产品类型"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="动作" prop="movement">
              <el-input v-model="form.movement" placeholder="请输入动作"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="来源单号" prop="trackingNumber">
              <el-input v-model="form.trackingNumber" placeholder="请输入来源单号"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="创建人" prop="createUser">
              <el-input v-model="form.createUser" placeholder="请输入创建人"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="最后更新人" prop="lastUpdater">
              <el-input v-model="form.lastUpdater" placeholder="请输入最后更新人"/>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24">
            <el-form-item label="最后更新时间" prop="lastUpdateTime">
              <el-date-picker clearable
                              v-model="form.lastUpdateTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择最后更新时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInventoryJournal,
  getInventoryJournal,
  delInventoryJournal,
  addInventoryJournal,
  updateInventoryJournal
} from "@/api/wareManagement/inventoryJournal";

export default {
  name: "InventoryJournal",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存流水查询表格数据
      inventoryJournalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        materialNumber: null,
        materialName: null,
        quantity: null,
        locationNumber: null,
        type: null,
        movement: null,
        trackingNumber: null,
        createUser: null,
        lastUpdater: null,
        lastUpdateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialNumber: [
          {required: true, message: "产品编号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    aa(value) {
      this.queryParams.quantity = value.replace(/\D/g, '');
    },
    bb(value) {
      this.form.quantity = value.replace(/\D/g, '');
    },
    /** 查询库存流水查询列表 */
    getList() {
      this.loading = true;
      listInventoryJournal(this.queryParams).then(response => {
        this.inventoryJournalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        journalId: null,
        materialNumber: null,
        materialName: null,
        quantity: null,
        locationNumber: null,
        type: null,
        movement: null,
        trackingNumber: null,
        createUser: null,
        createTime: null,
        lastUpdater: null,
        lastUpdateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.journalId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存流水查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const journalId = row.journalId || this.ids
      getInventoryJournal(journalId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存流水查询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.journalId != null) {
            updateInventoryJournal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventoryJournal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const journalIds = row.journalId || this.ids;
      this.$modal.confirm('是否确认删除库存流水查询编号为"' + journalIds + '"的数据项？').then(function () {
        return delInventoryJournal(journalIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wareManagement/inventoryJournal/export', {
        ...this.queryParams
      }, `inventoryJournal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
